import {color} from '../utils/theme';
import { myCity, queryWeather, query } from '../services/dashboard'
import { parse } from 'qs'

export default {
  namespace: 'dashboard',
  state: {
    weather: {
      city: '昆明',
      temperature: '5',
      name: '晴',
      icon: 'http://www.zuimeitianqi.com/res/icon/0_big.png',
      dateTime: new Date().format('MM-dd hh:mm')
    },
    sales: [],
    quote: {
      avatar: 'http://img.hb.aicdn.com/bc442cf0cc6f7940dcc567e465048d1a8d634493198c4-sPx5BR_fw236'
    },
    numbers: [{
      icon: 'pay-circle-o',
      color: color.green,
      title: 'Online Review',
      number: 2781
    }, {
      icon: 'team',
      color: color.blue,
      title: 'New Customers',
      number: 3241
    }, {
      icon: 'message',
      color: color.purple,
      title: 'Active Projects',
      number: 253
    }, {
      icon: 'shopping-cart',
      color: color.red,
      title: 'Referrals',
      number: 4324
    }],
    recentSales: [],
    comments: [],
    completed: [],
    browers: [],
    cpu: {},
    user: {
      avatar: 'http://img.hb.aicdn.com/bc442cf0cc6f7940dcc567e465048d1a8d634493198c4-sPx5BR_fw236'
    }
  },
  subscriptions: {
    setup({ dispatch }) {}
  },
  effects: { * query({ payload }, { call, put }) {
      const data = yield call(query, parse(payload));
      yield put({ type: 'queryWeather', payload: {...data } });
    },
    * queryWeather({ payload }, { call, put }) {
      const myCityResult = yield call(myCity, { flg: 0 });
      const myCityData = myCityResult.query.results.json;
      const result = yield call(queryWeather, { cityCode: myCityData.selectCityCode });
      const data = result.query.result.json;
      const weather = zuimei.ParseActualData(data.data.actual);
      weather.city = myCityData.selectCityName;
      yield put({ type: 'queryWeatherSuccess', payload: { weather } })
    }
  },
  reducers: {
    queryWeatherSuccess(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    queryWeather(state, action) {
      return {
        ...state,
        ...action.payload
      }
    }
  },
};
